package com.ope.web.controller.laterhandler;


import java.io.IOException;
import java.util.Arrays;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.ope.web.controller.chainresponsibility.TopicAnalysisHandle;
import com.ope.web.model.DTO.ProcessMointorRequestBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;




/**
 * 项目服务实现调度
 */
@Component
@Slf4j
public class TopicHandleAnalysisProject extends TopicAnalysisHandle {


    private static final String[] SERVICE_TYPE = {"project"};


    @Override
    public void process(ProcessMointorRequestBody processMointorRequestBody) {

        //处理上报数据
        dataReport(processMointorRequestBody);
    }


    /**
     * 项目服务实现调度
     * @param    processMointorRequestBody processMointorRequestBody
     * @author fxr
     * @date 2024年12月24日16:39:20
     */
    private void dataReport(ProcessMointorRequestBody processMointorRequestBody) {
        //进行对象拆解
        String hostname = "10.10.12.114";
        String username = "root";
        String password = "SPrixin.2024";
        String processToCheck = "model";
        String host = processMointorRequestBody.getHostname();
        String user = processMointorRequestBody.getUsername();
        String pass = processMointorRequestBody.getPassword();
        String processName = processMointorRequestBody.getProcessName();
        // 检查程序是否正在运行
        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(username, hostname, 22);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            // Check if the process is running
            String checkCommand = "pgrep -fl " + processToCheck;
            ChannelExec checkChannel = (ChannelExec) session.openChannel("exec");
            checkChannel.setCommand(checkCommand);

            checkChannel.setInputStream(null);
            checkChannel.setErrStream(System.err);

            try (java.io.InputStream in = checkChannel.getInputStream()) {
                checkChannel.connect();

                // If the process is not running, start it
                if (in.read() == -1) {
                    String command = processMointorRequestBody.getBatsh();
                    String startCommand = " /home/sprixin/sprixinSoft/jdk/bin/java -Dfile.encoding=utf-8"
                            + " -jar  -Dloader.path=/home/sprixin/sprixinWeb/web/jar/rxpg-model"
                            + "  /home/sprixin/sprixinWeb/web/jar/rxpg-model.xjar "
                            + "--spring.config.location=/home/sprixin/sprixinWeb/web/config/bootstrap-model.yml";
                    ChannelExec startChannel = (ChannelExec) session.openChannel("exec");
                    startChannel.setCommand(startCommand);
                    startChannel.setInputStream(null);
                    startChannel.setErrStream(System.err);

                    startChannel.connect();
                    startChannel.disconnect();
                } else {
                    System.out.println("66666666666666666666666666");
                }

                checkChannel.disconnect();
            }

            session.disconnect();

        } catch (JSchException | IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public String[] getServiceTypes() {
        return SERVICE_TYPE;
    }

    @Override
    public Boolean matchDecoder(ProcessMointorRequestBody processMointorRequestBody, String serviceType) {
        String[] productTypes = getServiceTypes();

        if (Arrays.asList(productTypes).contains(serviceType)) {
            return true;
        }

        return false;
    }

}
